package com.yunfeisoft.config.bean;

import com.yunfeisoft.common.file.FileOperation;
import com.yunfeisoft.common.file.LocalDiskFileOperation;
import com.yunfeisoft.common.serialize.HessianSerializer;
import com.yunfeisoft.common.serialize.Serializer;
import com.yunfeisoft.common.session.RedisSession;
import com.yunfeisoft.common.session.UserSession;
import com.yunfeisoft.common.session.WebDomainContext;
import com.yunfeisoft.common.sql.page.PageSql;
import com.yunfeisoft.common.sql.page.PostgresqlPage;
import com.yunfeisoft.common.sql.record.DomainModelContext;
import com.yunfeisoft.common.utils.RedisHelper;
import com.yunfeisoft.common.utils.SpringContextHelper;
import com.yunfeisoft.common.weixin.WXOpenApi;
import com.yunfeisoft.interceptor.LoginInterceptor;
import com.yunfeisoft.utils.WorkflowHelper;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 业务对象配置
 */
@Configuration
public class BeanConfig {

    /**
     * Spring上下文
     *
     * @return
     */
    @Bean
    public SpringContextHelper springContextHelper() {
        return new SpringContextHelper();
    }

    /**
     * 对象序列化策略
     *
     * @return
     */
    /*@Bean
    public Serializer serializer() {
        return new HessianSerializer();
    }*/

    /**
     * redis工具
     *
     * @return
     */
    @Bean
    public RedisHelper redisHelper(RedisTemplate<String, Object> redisTemplate) {
        RedisHelper redisHelper = new RedisHelper();
        redisHelper.setRedisTemplate(redisTemplate);
        return redisHelper;
    }

    /**
     * 对象序列化策略
     *
     * @return
     */
    @Bean
    public Serializer getSerializer() {
        return new HessianSerializer();
    }

    /**
     * 用户信息缓存策略 servlet
     *
     * @return
     */
    @Bean
    public UserSession userSession(RedisHelper redisHelper, Serializer serializer) {
        RedisSession session = new RedisSession();
        session.setRedisHelper(redisHelper);
        session.setSerializer(serializer);
        return session;
    }

    /**
     * 领域模型上下文
     *
     * @return
     */
    @Bean
    public DomainModelContext domainModelContext() {
        return new DomainModelContext();
    }

    /**
     * 分页查询方言，支持mysql、oracle、mysql
     *
     * @return
     */
    @Bean
    public PageSql pageSql() {
        return new PostgresqlPage();
    }

    /**
     * 分页查询方言，支持mysql、oracle、mysql
     *
     * @return
     */
    @ConfigurationProperties(prefix = "file.local")
    @Bean
    public FileOperation fileOperation() {
        return new LocalDiskFileOperation();
    }

    /**
     * 系统域名配置
     *
     * @return
     */
    @ConfigurationProperties(prefix = "server.servlet")
    @Bean
    public WebDomainContext getWebDomainContext() {
        return new WebDomainContext();
    }

    /**
     * 工作流帮助类
     *
     * @return
     */
    @Bean
    public WorkflowHelper getWorkflowHelper() {
        return new WorkflowHelper();
    }

    /**
     * 微信接口操作工具
     *
     * @return
     */
    @ConfigurationProperties(prefix = "wx")
    @Bean
    public WXOpenApi getWXOpenApi(RedisHelper redisHelper) {
        WXOpenApi wxOpenApi = new WXOpenApi();
        wxOpenApi.setRedisHelper(redisHelper);
        return wxOpenApi;
    }
}
